package ga.wavaplayer {
	
	import ga.wavaplayer.ui.WPlayerUI;
	import ga.wavaplayer.video.WVideo;
	import ga.wavaplayer.video.WHTTPVideo;
	import ga.wavaplayer.video.WRTMPVideo;
	import ga.wavaplayer.video.WVideoMode;
	import ga.wloader.events.WNetEvent;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import ga.wloader.WLoader;
	import ga.wemanager.WEManager;
		
	public class WAVAPlayer extends Sprite {
		
		private var _options:Object;
		private var _gui:WPlayerUI;
		private var _video:WVideo;
		private var _videoContainer:Sprite = new Sprite();
		
		private var _bg:Sprite = new Sprite;
		private var _bg_overlay:Bitmap;
		private var _loader:WLoader;

		public static const READY:String = "wavaplayer.ready";
		
		public function WAVAPlayer(options:Object = null): void {
			
			//	Set Default Options
			_options = {
				width: 852,
				height: 480,
				bg: {
					color: 0x3f845f,
					alpha: 1
				},
				video: {},
				ui: {}
			};
			
			//	Override default options with user speficied ones
			if (options.width != undefined) _options.width = options.width;
			if (options.height != undefined) _options.height = options.height;
			if (options.bg != undefined) {
				_options.bg = { };
				if (options.bg.color != undefined) _options.bg.color = options.bg.color;
				if (options.bg.alpha != undefined) _options.bg.alpha = options.bg.alpha;
			}
	
			//	Setup UI Options
			if (options.ui != undefined) _options.ui = options.ui;
			_options.ui.width = _options.width;
			_options.ui.height = _options.height;
			
			if (_options.ui.skin == undefined) _options.ui.skin = "default";
			if (_options.ui.skinPath == undefined) _options.ui.skinPath = "default";
			if (_options.ui.volume == undefined) _options.ui.volume = 0.6;
			
			//	Setup Video Options
			if (options.video != undefined) _options.video = options.video;
			
			//	Load Elements
			_loader = new WLoader("WAVAPlayer");
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/background_overlay.png", { id: "background_overlay" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/play.png", { id: "icon-play" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/pause.png", { id: "icon-pause" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/stop.png", { id: "icon-stop" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/maximize.png", { id: "icon-maximize" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/minimize.png", { id: "icon-minimize" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/next.png", { id: "icon-next" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/previous.png", { id: "icon-previous" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/sound_0.png", { id: "icon-sound_0" } );
			_loader.addURL(_options.ui.skinPath+"skins/"+_options.ui.skin+"/icons/sound_1.png", { id: "icon-sound_1" } );
			
			if(_options.video.background != undefined)
				_loader.addURL(options.video.background, { id: "background" } );
			
			_loader.addEventListener(ProgressEvent.PROGRESS, _loaderProgressHandler );
			_loader.addEventListener(WNetEvent.READY, _loaderReadyHandler);
			
			_loader.start();
		}
		
		public function resize(pWidth:int, pHeight:int ):void {
			_bg.graphics.clear();
			_bg.graphics.beginFill(_options.bg.color, _options.bg.alpha);
			_bg.graphics.drawRect(0, 0, pWidth, pHeight);
			_bg.graphics.endFill();
			
			_bg_overlay.width = pWidth;
			_bg_overlay.height = pHeight;
			
			_gui.resize(pWidth, pHeight);
			_video.resize(pWidth, pHeight);
		}
		
		private function _loaderReadyHandler(e:WNetEvent):void {
			
			//	Initiate Video Object
			_video = WVideo.getVideo(_options.video);
			
			//	Create UI
			_gui = new WPlayerUI(_video, _options.ui);
			
			//	Set BG Overlay
			_bg_overlay = _loader.getRequest("background_overlay").bitmap;
			
			if(_loader.hasLoaded("background"))
				_bg.addChild(_loader.getRequest("background").bitmap);
			
			//	Resize the player to specified dimensions
			resize(_options.width, _options.height);
			
			//	Set Event Listeners
			_videoContainer.buttonMode = true;
			_videoContainer.addEventListener(MouseEvent.CLICK, _videoContainerClickHandler);
			
			//	Display Objects
			_videoContainer.addChild(_bg);
			_videoContainer.addChild(_bg_overlay);
			_videoContainer.addChild(_video);

			this.addChild(_videoContainer);
			this.addChild(_gui);
			
			_video.play();
			
			//	Dispatch Ready Event
			this.dispatchEvent(new Event(WAVAPlayer.READY));
		}
		
		private function _loaderProgressHandler(e:ProgressEvent):void {
			
		}
		
		private function _videoContainerClickHandler(e:MouseEvent):void {
			trace('video container clicked');
		}
	}
}
